package com.masterclient.app.modules.income.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.dealer.model.DealerBankCardMoreBean;
import com.masterclient.app.modules.income.model.ApplyWithdrawBean;

import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class DealerBankCardInsertPresenter extends DealerBankCardInsertContract.Presenter{

    @Override
    public void getBankCardMoreInfo() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getBankCardMoreInfo(), (Consumer<CommonBaseResponse<DealerBankCardMoreBean>>) bean -> {
            if (bean.getCode() == 0){
                mView.getBankCardMoreInfoSuccess(bean.getData());
            }else {
                mView.getBankCardMoreInfoFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.getBankCardMoreInfoFail("网络异常，请重试");
        }));
    }

    @Override
    public void bindBankCard(String bank_id,String bank_account_no,String bank_mobile,String verify_code,String bind_type) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_cert_id",bank_id);
            jsonObject.put("bank_account_no",bank_account_no);
            jsonObject.put("bank_mobile",bank_mobile);
            jsonObject.put("bind_type",bind_type);
            jsonObject.put("verify_code",verify_code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse<ApplyWithdrawBean>> observable = RetrofitUtil2.getInstance().bindBankCard(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<ApplyWithdrawBean>>) bean -> {
            if(bean.getCode() == 0){
                mView.bindSuccess(bean.getData());
            }else {
                mView.bindFailed(bean.getMsg());
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void getCaptcha(String bank_id,String bank_account_no,String bank_mobile,String bind_type) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_cert_id",bank_id);
            jsonObject.put("bank_account_no",bank_account_no);
            jsonObject.put("bank_mobile",bank_mobile);
            jsonObject.put("bind_type",bind_type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse> observable = RetrofitUtil2.getInstance().getDealerCaptcha(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if(bean.getCode() == 0){
                mView.getCaptchaSuccess();
            }else {
                mView.getCaptchaFailed(bean.getMsg());
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}